package com.mock.ems.controller.distribution;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.mock.base.util.NumberUtil;
import com.mock.ems.common.JsonResult;

/**
 * Created by MOCK on 2017/7/27.
 */
public class DistributionController extends Controller {

    public void index() {

        Record record = Db.findFirst("select * from distribution_config");
        if (null != record) {

            formatRecord("merchant_ratio",record,true);
            formatRecord("platform_ratio",record,true);
            formatRecord("area_agent_ratio",record,true);
            formatRecord("service_center_agent_ratio",record,true);
            formatRecord("cooperation_partner_agent_ratio",record,true);
            formatRecord("recommender_ratio",record,true);
            formatRecord("consumer_ratio",record,true);


            formatRecord("consumer_score_ratio",record,false);
            formatRecord("merchant_score_return_ratio",record,false);
            formatRecord("consumer_exchange_ratio",record,false);
            formatRecord("recommender_exchange_ratio",record,false);

            setAttr("distributionConfig",record.getColumns());
        }
        renderJsp("/page/distribution/distribution_dividends_config.jsp");
    }

    public void update() {

        String updateKey = getPara("updateKey");
        String updateValue = getPara("updateValue");
        Double upValue = 0.00;
        if (updateValue.contains("%")) {
            updateValue = updateValue.substring(0,updateValue.length() - 1);
            upValue = NumberUtil.toDouble(updateValue)/100;

        }else {
            upValue = NumberUtil.toDouble(updateValue.split(":")[1]);
        }

        String sql = "update distribution_config set " + updateKey + " = ? where id = 1";
        int updateRows = Db.update(sql,upValue);
        if (updateRows <= 0) {
            renderJson(new JsonResult().status("-1").show("更新失败"));
            return;
        }
        renderJson(new JsonResult().status("000"));
    }

    public void reset() {

        String updateKey = getPara("updateKey");
        String udValue = getPara("updateValue");

        String sql = "update distribution_config set " + updateKey + " = ? where id = 1";
        Record resetRecord = Db.findById("distribution_config",1000000);
        Double updateValue = resetRecord.getDouble(updateKey);
        int updateRows = Db.update(sql,updateValue);
        if (updateRows <= 0) {
            renderJson(new JsonResult().status("-1").show("重置失败"));
            return;
        }

        //格式化返回
        boolean percentFormat = udValue.contains("%");
        String data = percentFormat ? formatValue(updateValue,percentFormat) : "1:" + formatValue(updateValue,percentFormat);
        renderJson(new JsonResult().status("000").data(data));
    }

    /**
     * 格式化配置
     * @param name
     * @param record
     * @param percentFormat
     */
    private void formatRecord(String name,Record record,boolean percentFormat) {
        double value = record.getDouble(name);
        if (percentFormat) {
            value *= 100;
        }
        if (value == (int)value) {
            record.set(name,percentFormat ? (int)value + "%" : (int)value);
        }else {
            record.set(name, NumberUtil.formateDouble(2,value)+ "%");
        }
    }

    private String formatValue(double value,boolean percentFormat) {
        if (percentFormat) {
            value *= 100;
        }
        if (value == (int)value) {
            return percentFormat ? (int)value + "%" : String.valueOf((int)value);
        }else {
            return NumberUtil.formateDouble(2,value)+ "%";
        }
    }

}
